package socketio.server;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import org.redisson.Config;
import org.redisson.Redisson;

/**
 * Created by alex on 16-1-14.
 */
public class RedissonChatLauncher {

    public static void main(String[] args) throws InterruptedException {

        Config config = new Config();
        config.useSingleServer().setAddress("127.0.0.1:6379").setDatabase(1);
        Redisson redisson = (Redisson) Redisson.create(config);

        RedissonStoreFactory redissonStoreFactory = new RedissonStoreFactory(redisson);

        Configuration configuration = new Configuration();
        configuration.setHostname("0.0.0.0");
        configuration.setPort(9092);
        configuration.setOrigin("http://192.168.1.65:3000");
        configuration.setStoreFactory(redissonStoreFactory);

        final SocketIOServer server = new SocketIOServer(configuration);
        server.addEventListener("message", ChatObject.class, new DataListener<ChatObject>() {
            @Override
            public void onData(SocketIOClient client, ChatObject data, AckRequest ackSender) throws Exception {
                server.getBroadcastOperations().sendEvent("message", data);

            }
        });

        server.addEventListener("join_room", String.class, new DataListener<String>() {
            @Override
            public void onData(SocketIOClient client, String data, AckRequest ackSender) throws Exception {
                client.joinRoom(data);
                if (ackSender.isAckRequested()) {
                    ackSender.sendAckData("join " + data + " success");
                }
            }
        });

        server.addEventListener("leave_room", String.class, new DataListener<String>() {
            @Override
            public void onData(SocketIOClient client, String data, AckRequest ackSender) throws Exception {
                client.leaveRoom(data);
                if (ackSender.isAckRequested()) {
                    ackSender.sendAckData("leave " + data + " success");
                }
            }
        });

        server.addEventListener("room_message", String.class, new DataListener<String>() {
            @Override
            public void onData(SocketIOClient client, String data, AckRequest ackSender) throws Exception {
                server.getRoomOperations("room1").sendEvent("room_message", data);
            }
        });

        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient client) {
                System.out.println("sessionId: " + client.getSessionId() + " ---> " + client.getTransport());
            }
        });

        server.addDisconnectListener(new DisconnectListener() {
            @Override
            public void onDisconnect(SocketIOClient client) {
                System.err.println("sessionId: " + client.getSessionId() + " ---> " + client.getTransport());
            }
        });

        server.start();
        Thread.sleep(3000000000l);
        server.stop();
    }
}